package com.gitpractice.liwushuo.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.android.volley.Request;
import com.android.volley.Response;
import com.gitpractice.liwushuo.R;
import com.gitpractice.liwushuo.activity.SearchActivity;
import com.gitpractice.liwushuo.adapter.HotAdapter;
import com.gitpractice.liwushuo.bean.hotBean;
import com.gitpractice.liwushuo.contanst.UrlString;
import com.gitpractice.liwushuo.utils.VolleyUtils;

import java.util.ArrayList;
import java.util.List;

import in.srain.cube.views.ptr.PtrClassicFrameLayout;
import in.srain.cube.views.ptr.PtrDefaultHandler;
import in.srain.cube.views.ptr.PtrFrameLayout;


public class SelectFragment extends Fragment {
    private RecyclerView recyclerView;
    private PtrClassicFrameLayout ptrClassicFrameLayout;
    private List<hotBean.ItemBean> mDatas;
    private HotAdapter mAdapter;
    private String next_url = UrlString.URL_HOT;
    private GridLayoutManager mGridLayoutManager;
    private ImageView iv_search;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_select, container, false);
        recyclerView = (RecyclerView) view.findViewById(R.id.rv_recycler);
        iv_search= (ImageView) view.findViewById(R.id.iv_search_selecte);
        ptrClassicFrameLayout= (PtrClassicFrameLayout) view.findViewById(R.id.fl);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        iv_search= (ImageView) view.findViewById(R.id.iv_search_selecte);
        iv_search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(getActivity(), SearchActivity.class));
            }
        });

    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mDatas = new ArrayList<>();
        mAdapter = new HotAdapter(getContext(), mDatas);
        recyclerView.setAdapter(mAdapter);
        mGridLayoutManager = new GridLayoutManager(getContext(), 2, LinearLayoutManager.VERTICAL, false);
        recyclerView.setLayoutManager(mGridLayoutManager);
        loadData();
      /*  iv_search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(getContext(), SearchActivity.class));
            }
        });
*/
        ptrClassicFrameLayout.setResistance(1.7f);
        ptrClassicFrameLayout.setPullToRefresh(true);
        ptrClassicFrameLayout.setRatioOfHeaderHeightToRefresh(2f);
        ptrClassicFrameLayout.setKeepHeaderWhenRefresh(true);
        ptrClassicFrameLayout.setDurationToClose(2000);
        ptrClassicFrameLayout.setDurationToCloseHeader(1000);

        ptrClassicFrameLayout.setPtrHandler(new PtrDefaultHandler() {
            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                ptrClassicFrameLayout.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        // mDatas.clear();
                        loadData();
                        mAdapter.notifyDataSetChanged();
                        ptrClassicFrameLayout.refreshComplete();
                    }
                },2000);
            }
        });

        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                int itemCount = mGridLayoutManager.getItemCount();
                int lastVisibleItemPosition = mGridLayoutManager.findLastVisibleItemPosition();
                if ((lastVisibleItemPosition + 1 == itemCount) && (newState == RecyclerView.SCROLL_STATE_IDLE)) {
                    loadData();
                }
            }
        });
    }


    private void loadData() {
        VolleyUtils.newInstance(getContext()).newGsonRequest(Request.Method.GET, next_url,
                new Response.Listener<hotBean>() {
                    @Override
                    public void onResponse(hotBean response) {
                        next_url = response.data.paging.next_url;
                        Log.i("123", next_url);
                        mDatas.addAll(response.data.items);
                        Log.i("123", mDatas + "");
                        mAdapter.notifyDataSetChanged();
                    }
                }, null, hotBean.class);
    }
}
